home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / internet config assistant / toolkit / tdrawcontext.cp < prev    next >
Encoding:
Text File  |  1996-06-30  |  11.3 KB  |  495 lines

  1. /*
  2.     File:        TDrawContext.cp
  3.  
  4.     Contains:    Implementation of a layer class on top of QuickDraw
  5.  
  6.     Written by:    Arno Gourdol
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #include "TDrawContext.h"
  13. #include "TBitmap.h"
  14.  
  15. #include "macros.h"
  16.  
  17.  
  18.  
  19. // --------------------------------------------------------------------
  20. // MovePenTo
  21. // --------------------------------------------------------------------
  22.  
  23. void TDrawContext::MovePenTo(CPoint pt)
  24. {
  25.     assert(fLockCount > 0);
  26.  
  27.     ::MoveTo(pt.X(), pt.Y());
  28. }
  29.  
  30.  
  31.  
  32. // --------------------------------------------------------------------
  33. // MovePenTo
  34. // --------------------------------------------------------------------
  35.  
  36. void TDrawContext::MovePenTo(GraphicalUnit x, GraphicalUnit y)
  37. {
  38.     assert(fLockCount > 0);
  39.  
  40.     ::MoveTo(x, y);
  41. }
  42.  
  43.  
  44.  
  45. // --------------------------------------------------------------------
  46. // MovePenBy
  47. // --------------------------------------------------------------------
  48.  
  49. void TDrawContext::MovePenBy(GraphicalUnit dx, GraphicalUnit dy)
  50. {
  51.     assert(fLockCount > 0);
  52.  
  53.     ::Move(dx, dy);
  54. }
  55.  
  56.  
  57.  
  58. // --------------------------------------------------------------------
  59. // PenLocation
  60. // --------------------------------------------------------------------
  61.  
  62. CPoint TDrawContext::PenLocation(void) const
  63. {
  64.     assert(fLockCount > 0);
  65.  
  66.     PenState penState;
  67.     ::GetPenState(&penState);
  68.     return CPoint(penState.pnLoc);
  69. }
  70.  
  71.  
  72.  
  73. // --------------------------------------------------------------------
  74. // StrokeLine
  75. // --------------------------------------------------------------------
  76.  
  77. void TDrawContext::StrokeLine(CPoint toPt)
  78. {
  79.     assert(fLockCount > 0);
  80.  
  81.     ::LineTo(toPt.X(), toPt.Y());
  82. }
  83.  
  84.  
  85.  
  86. // --------------------------------------------------------------------
  87. // StrokeLine
  88. // --------------------------------------------------------------------
  89.  
  90. void TDrawContext::StrokeLine(CPoint pt0, CPoint pt1)
  91. {
  92.     assert(fLockCount > 0);
  93.  
  94.     ::MoveTo(pt0.X(), pt0.Y());
  95.     ::LineTo(pt1.X(), pt1.Y());
  96. }
  97.  
  98.  
  99.  
  100. // --------------------------------------------------------------------
  101. // StrokeRect
  102. // --------------------------------------------------------------------
  103.  
  104. void TDrawContext::StrokeRect(const CRect& rect)
  105. {
  106.     assert(fLockCount > 0);
  107.  
  108.     ::FrameRect(rect);
  109. }
  110.  
  111.  
  112.  
  113. // --------------------------------------------------------------------
  114. // FillRect
  115. // --------------------------------------------------------------------
  116.  
  117. void TDrawContext::FillRect(const CRect& rect)
  118. {
  119.     assert(fLockCount > 0);
  120.  
  121.     ::PaintRect(rect);
  122. }
  123.  
  124.  
  125.  
  126. // --------------------------------------------------------------------
  127. // InvertRect
  128. // --------------------------------------------------------------------
  129.  
  130. void TDrawContext::InvertRect(const CRect& rect)
  131. {
  132.     assert(fLockCount > 0);
  133.  
  134.     ::InvertRect(rect);
  135. }
  136.  
  137.  
  138. /*
  139.  
  140.  
  141.  
  142. // --------------------------------------------------------------------
  143. // MovePenTo
  144. // --------------------------------------------------------------------
  145.  
  146. void TDrawContext::FrameInsetBox(const CRect& rect) const
  147. {
  148.     CRect tempRect(rect);
  149.  
  150.     InsetRect(tempRect, 1, 1);
  151.     this->Frame(tempRect);
  152.     InsetRect(tempRect, -1, -1);
  153.  
  154.     this->LineFromTo(tempRect.Left(), tempRect.Bottom() - 2, tempRect.Left(), tempRect.Top(), kInsetBoxShadow);
  155.     this->LineTo(tempRect.Right() - 2, tempRect.Top(), kInsetBoxShadow);
  156.  
  157.  
  158.     this->LineFromTo(tempRect.Right() - 1, tempRect.Top() + 1, tempRect.Right() - 1, tempRect.Bottom() - 1, kInsetBoxHilite);
  159.     this->LineTo(tempRect.Left(), tempRect.Bottom() - 1, kInsetBoxHilite);
  160.  
  161.     ::ForeColor(blackColor);
  162. }
  163. */
  164.  
  165. /*
  166.  
  167.  
  168.  
  169. // --------------------------------------------------------------------
  170. // MovePenTo
  171. // --------------------------------------------------------------------
  172.  
  173. void TDrawContext::FrameBevel(const CRect& rect,
  174.                            GraphicalUnit leftOffset,
  175.                            GraphicalUnit rightOffset)
  176. {
  177.     CRect tempRect(rect);
  178.  
  179.     leftOffset = pin(rect.Left() + leftOffset, rect.Left(), rect.Right()) - rect.Left();
  180.     rightOffset = rect.Right() - pin(rect.Right() - rightOffset, rect.Left(), rect.Right());
  181.  
  182.     if (leftOffset == rightOffset)
  183.     {
  184.         SetHighColor(CColor(kBevelShadowColor));
  185.         StrokeLine(tempRect.LeftTop(), CPoint(tempRect.Right() - 2, tempRect.Top()));
  186.         StrokeLine(CPoint(tempRect.Right() - 2, tempRect.Bottom() - 2));
  187.         StrokeLine(CPoint(tempRect.Left(), tempRect.Bottom() - 2));
  188.         StrokeLine(CPoint(tempRect.Left(), tempRect.Top()));
  189.  
  190.         // Left side
  191.         SetHighColor(CColor(kBevelHiliteColor));
  192.         MovePenTo(tempRect.Left() + 1, tempRect.Bottom() - 3);
  193.         StrokeLine(CPoint(tempRect.Left() + 1, tempRect.Top() + 1));
  194.  
  195.         // Top side
  196.         StrokeLine(CPoint(tempRect.Right() - 3, tempRect.Top() + 1));
  197.  
  198.         // Right side
  199.         MovePenTo(CPoint(tempRect.Right() - 1, tempRect.Top()));
  200.         StrokeLine(CPoint(tempRect.Right() - 1, tempRect.Bottom() - 1));
  201.  
  202.         // Bottom side
  203.         StrokeLine(CPoint(tempRect.Left(), tempRect.Bottom() - 1));
  204.     }
  205.     else
  206.     {
  207.         // Top left side
  208.         this->LineFromTo(tempRect.Left(), tempRect.Top(), tempRect.Left() + leftOffset, tempRect.Top(), kBevelShadowIndex);
  209.         this->LineFromTo(tempRect.Left() + 1, tempRect.Top() + 1, tempRect.Left() + leftOffset, tempRect.Top() + 1, kWhiteIndex);
  210.  
  211.         // Top right side
  212.         this->LineFromTo(tempRect.Right() - rightOffset, tempRect.Top(), tempRect.Right() - 1, tempRect.Top(), kBevelShadowIndex);
  213.         this->LineFromTo(tempRect.Right() - rightOffset, tempRect.Top() + 1, tempRect.Right() - 2, tempRect.Top() + 1, kWhiteIndex);
  214.  
  215.         // Left side
  216.         this->LineFromTo(tempRect.Left(), tempRect.Top(), tempRect.Left(), tempRect.Bottom() - 1, kBevelShadowIndex);
  217.         this->LineFromTo(tempRect.Left() + 1, tempRect.Top() + 1, tempRect.Left() + 1, tempRect.Bottom() - 2, kWhiteIndex);
  218.  
  219.         // Right side
  220.         this->LineFromTo(tempRect.Right() - 1, tempRect.Top(), tempRect.Right() - 1, tempRect.Bottom() - 1, kBevelShadowIndex);
  221.         this->LineFromTo(tempRect.Right(), tempRect.Top(), tempRect.Right(), tempRect.Bottom(), kWhiteIndex);
  222.  
  223.         // Bottom side
  224.         this->LineFromTo(tempRect.Left(), tempRect.Bottom() - 1, tempRect.Right() - 1, tempRect.Bottom() - 1, kBevelShadowIndex);
  225.         this->LineFromTo(tempRect.Left(), tempRect.Bottom(), tempRect.Right(), tempRect.Bottom(), kWhiteIndex);
  226.     }
  227.  
  228.     ::ForeColor(blackColor);
  229. }
  230.  
  231. */
  232.  
  233.  
  234.  
  235. // --------------------------------------------------------------------
  236. // StrokeRegion
  237. // --------------------------------------------------------------------
  238.  
  239. void TDrawContext::StrokeRegion(const RgnHandle rgn)
  240. {
  241.     assert(fLockCount > 0);
  242.  
  243.     ::FrameRgn(rgn);
  244. }
  245.  
  246.  
  247.  
  248. // --------------------------------------------------------------------
  249. // FillRegion
  250. // --------------------------------------------------------------------
  251.  
  252. void TDrawContext::FillRegion(const RgnHandle rgn)
  253. {
  254.     assert(fLockCount > 0);
  255.  
  256.     ::PaintRgn(rgn);
  257. }
  258.  
  259.  
  260.  
  261. // --------------------------------------------------------------------
  262. // DrawBitmap
  263. // --------------------------------------------------------------------
  264.  
  265. void TDrawContext::DrawBitmap(const TBitmap* bitmap) const
  266. {
  267.     assert(bitmap != NULL);
  268.     GWorldPtr grafWorld = bitmap->GetGWorldPtr();
  269.     
  270.     if (grafWorld != NULL)
  271.     {
  272.         ::CopyBits(&((GrafPtr)grafWorld)->portBits,
  273.                     &fPort->portBits, bitmap->Bounds(), 
  274.                     bitmap->Bounds(), srcCopy, NULL);
  275.     }
  276. }
  277.  
  278.  
  279.  
  280. // --------------------------------------------------------------------
  281. // DrawBitmap
  282. // --------------------------------------------------------------------
  283.  
  284. void TDrawContext::DrawBitmap(const TBitmap* bitmap, CPoint where) const
  285. {
  286. #pragma unused(bitmap, where)
  287.  
  288.  
  289. }
  290.  
  291.  
  292.  
  293. // --------------------------------------------------------------------
  294. // DrawBitmap
  295. // --------------------------------------------------------------------
  296.  
  297. void TDrawContext::DrawBitmap(const TBitmap* bitmap, CRect dstRect, 
  298.                                                     RgnHandle mask) const
  299. {
  300.     assert(bitmap != NULL);
  301.     GWorldPtr grafWorld = bitmap->GetGWorldPtr();
  302.     
  303.     if (grafWorld != NULL)
  304.     {
  305.         ::CopyBits(&((GrafPtr)grafWorld)->portBits,
  306.                 &fPort->portBits, bitmap->Bounds(), 
  307.                 dstRect, srcCopy, mask);
  308.     }
  309. }
  310.  
  311.  
  312.  
  313. // --------------------------------------------------------------------
  314. // MovePenTo
  315. // --------------------------------------------------------------------
  316.  
  317. void TDrawContext::DrawBitmap(const TBitmap* bitmap, CRect dstRect) const
  318. {
  319.     assert(bitmap != NULL);
  320.     GWorldPtr grafWorld = bitmap->GetGWorldPtr();
  321.     
  322.     if (grafWorld != NULL)
  323.     {
  324.         ::CopyBits(&((GrafPtr)grafWorld)->portBits,
  325.                 &fPort->portBits, bitmap->Bounds(), 
  326.                 dstRect, srcCopy, NULL);
  327.     }
  328. }
  329.  
  330.  
  331.  
  332. // --------------------------------------------------------------------
  333. // MovePenTo
  334. // --------------------------------------------------------------------
  335.  
  336. void TDrawContext::DrawBitmap(const TBitmap* bitmap, CRect srcRect, CRect dstRect) const
  337. {
  338.     assert(bitmap != NULL);
  339.     GWorldPtr grafWorld = bitmap->GetGWorldPtr();
  340.     
  341.     if (grafWorld != NULL)
  342.     {
  343.         ::CopyBits(&((GrafPtr)grafWorld)->portBits,
  344.                 &fPort->portBits, srcRect, 
  345.                 dstRect, srcCopy, NULL);
  346.     }
  347. }
  348.  
  349.  
  350.  
  351. // --------------------------------------------------------------------
  352. // MovePenTo
  353. // --------------------------------------------------------------------
  354.  
  355. void TDrawContext::StrokeFocusRing(const CRect& rect)
  356. {
  357.     assert(fLockCount > 0);
  358.  
  359.     CTempRgn haloRgn;
  360.  
  361.     // Temp rect and its pointer for Quickdraw API calls
  362.     CRect tempRect(rect);
  363.  
  364.     tempRect.InsetBy(-4);
  365.  
  366.     // Build the region of the halo
  367.     ::OpenRgn();
  368.     StrokeRect(tempRect);
  369.     tempRect.InsetBy(2);
  370.     StrokeRect(tempRect);
  371.     ::CloseRgn(haloRgn);
  372.  
  373.     FillRegion(haloRgn);
  374. }
  375.  
  376.  
  377.  
  378. // --------------------------------------------------------------------
  379. // MovePenTo
  380. // --------------------------------------------------------------------
  381.  
  382. void TDrawContext::StrokeFocusRing(const RgnHandle rgn)
  383. {
  384.     assert(fLockCount > 0);
  385.  
  386.     CTempRgn haloRgn = rgn;
  387.     CTempRgn tempRgn = rgn;
  388.  
  389.     ::InsetRgn(haloRgn, -4, -4);
  390.     ::InsetRgn(tempRgn, -2, -2);
  391.     ::DiffRgn(haloRgn, tempRgn, haloRgn);
  392.     
  393.     FillRegion(haloRgn);
  394. }
  395.  
  396.  
  397.  
  398. // --------------------------------------------------------------------
  399. // MovePenTo
  400. // --------------------------------------------------------------------
  401.  
  402. void TDrawContext::SetHighColor(CColor color)
  403. {
  404.     assert(fLockCount > 0);
  405.  
  406.     if (!color.IsBlack() && !color.IsWhite())
  407.     {
  408.         if (!IsColorPort())
  409.         {
  410.             ::ForeColor(whiteColor);
  411.         }
  412.         else
  413.         {            
  414.             if ((fDepth >= 8) || (fDepth == 4 && !fIsColor))
  415.             {
  416.                 ::RGBForeColor(color);
  417.             }
  418.             else
  419.             {
  420.                 ::ForeColor(whiteColor);            // not good enough for anything but white
  421.             }
  422.         }
  423.     }
  424.     else
  425.     {
  426.         ::RGBForeColor(color);
  427.     }
  428. }
  429.  
  430.  
  431.  
  432. // --------------------------------------------------------------------
  433. // Lock
  434. // --------------------------------------------------------------------
  435. // Call to start drawing in the drawing context.
  436. // If returns true, you can go ahead. 
  437. // Call Unlock() when done drawing.
  438.  
  439. Boolean TDrawContext::Lock(void)
  440. {
  441.     Boolean result;
  442.  
  443.     if (fLockCount == 0)
  444.     {
  445.         ::GetPort(&fSavePort);
  446.         if (fSavePort != NULL)
  447.         {
  448.             ::GetPenState(&fSavePenState);
  449.             ::GetForeColor(&fSaveForeColor);
  450.             ::GetBackColor(&fSaveBackColor);
  451.         }
  452.         assert(fPort != NULL);
  453.         ::SetPort(fPort);
  454.         ::PenNormal();
  455.         
  456.         result = true;
  457.     }
  458.     else
  459.     {
  460.         result = true;
  461.     }
  462.     
  463.     fLockCount++;
  464.  
  465.     return result;
  466. }
  467.  
  468.  
  469.  
  470. // --------------------------------------------------------------------
  471. // Unlock
  472. // --------------------------------------------------------------------
  473. // Call when done drawing in the drawing context.
  474. // Lock() must have been called before and have returned true.
  475.  
  476. void TDrawContext::Unlock(void)
  477. {
  478.     assert(fLockCount > 0);        // Make sure we were locked
  479.     
  480.     fLockCount--;
  481.     
  482.     if (fLockCount == 0)
  483.     {
  484.         SetPort(fSavePort);
  485.         if (fSavePort != NULL)
  486.         {
  487.             SetPenState(&fSavePenState);
  488.             RGBForeColor(&fSaveForeColor);
  489.             RGBBackColor(&fSaveBackColor);
  490.             fSavePort = NULL;
  491.         }    
  492.     }
  493. }
  494.  
  495.